package com.example.communist_app_api.controller.web;


import com.example.communist_app_api.db.pojoDto.Comment;
import com.example.communist_app_api.db.pojoDto.User;
import com.example.communist_app_api.db.pojoDto.UserSoncomment;
import com.example.communist_app_api.service.*;
import com.example.communist_app_api.util.JwtUtils;
import com.example.communist_app_api.util.R;
import com.example.communist_app_api.vo.uniapp.req.CommentReq;
import com.example.communist_app_api.vo.web.resp.SensitiveWordApiResp;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/comment/")
@Api("评论controller控制器")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @Autowired
    private WordService wordService;

    @Autowired
    private UserSoncommentService userSoncommentService;

    @Autowired
    private UserService userService;

    @Autowired
    private UserCommentLikeService userCommentLikeService;


    @GetMapping("/getUnmoderatdeComment")
    @ApiOperation("获取未经审核的评论")
    public R getUnmoderatdeComment(){
        List<Comment> commentList = commentService.getUnmoderatdeComment();
        if (commentList != null)
        {
            return R.ok().put("commentList",commentList);
        }
        else
            return R.error();
    }

    @PostMapping("/reviewComments")
    @ApiOperation("审核评论")
    public R reviewComments(HttpServletRequest httpServletRequest,Integer id){
        Integer userId = getUserIdByWeb(httpServletRequest);
        boolean flag = commentService.reviewComments(userId,id);
        if (flag == true){
            return R.ok();
        }
        else return R.error();
    }


    @GetMapping("getUserUnmoderatdeComment")
    @ApiOperation("获取未经审核的子评论")
    public R getUserUnmoderatdeComment(){
        List<UserSoncomment> list = userSoncommentService.getUnmoderatdeComment();
        if (list != null){
            return R.ok().put("list",list);
        }
        else return R.ok("无未审核的子评论");
    }

    @PostMapping("reviewUserComments")
    @ApiOperation("审核子评论")
    public R reviewUserComments(HttpServletRequest httpServletRequest,Integer id){
        Integer userId = getUserIdByWeb(httpServletRequest);
        if (userSoncommentService.reviewComments(userId, id)){
            return R.ok();
        }
        else return R.error();
    }

    @PostMapping("deleteComment")
    @ApiOperation("删除评论")
    public R deleteComment(Integer id)
    {
        if (commentService.deleteLikeNum(id))
        {
            return R.ok();
        }
        else
            return R.error();
    }

    @PostMapping("deleteUserComment")
    @ApiOperation("删除子评论")
    public R deleteUserComment(Integer id)
    {
        if (userSoncommentService.deleteUserComment(id))
        {
            return R.ok();
        }
        else return R.error();
    }

    @GetMapping("/getComment")
    @ApiOperation("获取所有已审核父评论")
    public R getComment()
    {
        List<Comment> list = commentService.getComment();
        if (list != null){
            return R.ok().put("list",list);
        }
        else return R.error();
    }

    @GetMapping("/getAllUserComment")
    @ApiOperation("获取所有已审核子评论")
    public  R getAllUserComment()
    {
        List<UserSoncomment> userSoncommentList = userSoncommentService.getAllComment();
        if (userSoncommentList != null)
        {
            return R.ok().put("userSoncommentList",userSoncommentList);
        }
        else return R.error();
    }


    private Integer getUserIdByWeb(HttpServletRequest httpServletRequest){
        String token = httpServletRequest.getHeader("token");
        Claims claims = JwtUtils.parseJWT(token);
        String userName = claims.getSubject();
        Integer userIdByWeb = userService.getUserInfo(userName).getId();
        return userIdByWeb;
    }




}
